Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Configuration.ConfigurationSettings
Imports System.IO
Imports System.Data

Public Class CReports
    Private Param As Collection

    Public Enum RptType
        Excel_Report = 1
        PDF_Report = 2
    End Enum

    Public Sub New()
        Param = New Collection
    End Sub

    Public Sub AddRptParameter(ByVal strParam)
        Param.Add(strParam)
    End Sub

    Public Sub ShowRpt(ByVal RptName As String, ByVal ReportType As RptType, ByVal Parent As Page, ByVal dt As DataTable, ByVal tblName As String)
        Dim exportOptions As New ExportOptions
        Dim diskFileDestinationOptions As New DiskFileDestinationOptions()

        Select Case ReportType
            Case RptType.Excel_Report
                Dim formatTypeOptions As New ExcelFormatOptions
                exportOptions.FormatOptions = formatTypeOptions
            Case RptType.PDF_Report
                Dim formatTypeOptions As New PdfRtfWordFormatOptions()
                exportOptions.FormatOptions = formatTypeOptions
        End Select

        Dim report As New ReportDocument()
        Dim strExtn As String = ""
        Dim strFile As String = ""

        With report
            'Dim ExportPath As String = AppSettings("ExportPath")
            Dim strPath As String = AppSettings("cRptPath")
            'Dim strUID As String = AppSettings("UID")
            'Dim strPassword As String = AppSettings("Password")
            'Dim strServer As String = AppSettings("Server")
            'Dim strDatabase As String = AppSettings("Database")


            .Load(strPath & "\" & RptName & ".rpt")

            dt.TableName = tblName
            .SetDataSource(dt)

        End With

        Select Case ReportType
            Case RptType.Excel_Report
                exportOptions.ExportFormatType = ExportFormatType.Excel
            Case RptType.PDF_Report
                exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
        End Select

        Dim req As ExportRequestContext = New ExportRequestContext
        req.ExportInfo = exportOptions

        Dim st As System.IO.Stream
        st = report.FormatEngine.ExportToStream(req)

        Parent.Response.ClearContent()
        Parent.Response.ClearHeaders()

        Select Case ReportType
            Case RptType.Excel_Report
                Parent.Response.ContentType = "application/vnd.ms-excel"
            Case RptType.PDF_Report
                Parent.Response.ContentType = "application/pdf"
        End Select

        Dim b(st.Length) As Byte
        st.Read(b, 0, st.Length) 'Read Stream
        Parent.Response.BinaryWrite(b) 'Write Stream
        st.Close() 'Close Stream
        st = Nothing 'Dispose

        Parent.Response.End()
    End Sub

    Protected Overrides Sub Finalize()
        Param = Nothing
        MyBase.Finalize()
    End Sub
End Class
